package com.huarui.area.action;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.util.HttpUtil;

import com.huarui.area.model.Area;
import com.huarui.area.service.AreaService;
import com.huarui.params.Params;

public class AreaAction extends BaseAction{
	
	private static final long serialVersionUID = -5010509888548814800L;
	
	private AreaService areaService;
	private Map<String, Object> defaultMap;
	private Integer areaId;

	private Integer start;
	private String queryParam;
	
	private Map<String, Object> mesMap;
	
	public Integer getAreaId() {
		return areaId;
	}

	public void setAreaId(Integer areaId) {
		this.areaId = areaId;
	}

	public void setAreaService(AreaService areaService) {
		this.areaService = areaService;
	}
	
	public Map<String, Object> getDefaultMap() {
		return defaultMap;
	}

	public void setDefaultMap(Map<String, Object> defaultMap) {
		this.defaultMap = defaultMap;
	}

	
	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public String getQueryParam() {
		return queryParam;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

	public Map<String, Object> getMesMap() {
		return mesMap;
	}

	public void setMesMap(Map<String, Object> mesMap) {
		this.mesMap = mesMap;
	}

	private JSONArray getDistsByCityId(String param) {
		int distId = Integer.parseInt(param);
		List<Area> areas = this.areaService.getDistsByCityId(distId);
		JSONArray ja = JSONArray.fromObject(areas);
		return ja;
	}
	
	private JSONObject getCitesJo(String param) {
		JSONObject jo = new JSONObject();
		jo.put(Params.AREA_AREAS_PARAM, this.getCitesByProvId(param));
		return jo;
	}
	
	private JSONArray getCitesByProvId(String param) {
		int cityId = Integer.parseInt(param);
		List<Area> areas = this.areaService.getCitesByProvId(cityId);
		JSONArray ja = JSONArray.fromObject(areas);
		return ja;
	}
	
	private JSONObject getDistsJo(String param) {
		JSONObject jo = new JSONObject();
		jo.put(Params.AREA_AREAS_PARAM, this.getDistsByCityId(param));
		return jo;
	}
	
	private JSONObject getErrorMesByNullId() {
		JSONObject jo = new JSONObject();
		jo.put("result", "-1");
		return jo;
	}
	
	public void getAreaByLastNevelId() throws Exception {
		Thread.sleep(500);
		HttpServletRequest req = HttpUtil.getRequest();
//		Set<Entry> set = req.getParameterMap().entrySet();
//		for(Entry e : set) {
//			System.out.println(e.getKey() + "   " + ((String[])e.getValue())[0] );
//		}
		PrintWriter out = this.getPrintWrite();
		String mes = null;
		if(req.getParameter(Params.AREA_PROV_PARAM) == null && req.getParameter(Params.AREA_CITY_PARAM) == null) {
//			mes = this.getProvices().toString();
			mes = this.getProvicesJo().toString();
			out.print(mes);
			return;
		}
		String str_id = null;
		str_id = req.getParameter(Params.AREA_PROV_PARAM);
		if(str_id == null || "".equals(str_id.trim())) {
			str_id = req.getParameter(Params.AREA_CITY_PARAM);
			if(str_id == null || "".equals(str_id.trim())) {
				mes = this.getErrorMesByNullId().toString();
			} else {
//				mes = this.getDistsByCityId(str_id.trim()).toString();
				mes = this.getDistsJo(str_id.trim()).toString();
			}
		} else {
//			mes = this.getCitesByProvId(str_id.trim()).toString();
			mes = this.getCitesJo(str_id.trim()).toString();
		}
		out.print(mes);
	}
	
	private JSONArray getProvices() {
		List<Area> provs = this.areaService.getProvs();
		JSONArray ja = JSONArray.fromObject(provs);
		return ja;
	}
	
	private JSONObject getProvicesJo() {
		JSONObject jo = new JSONObject();
		JSONArray ja = this.getProvices();
		jo.put(Params.AREA_AREAS_PARAM, ja);
		return jo;
	}
	
	/**
	 * 省市区有默认值的查询
	 * @return
	 */
	public String getDefaultValue() {
		defaultMap = new HashMap<String, Object>();
		List<Area> provs = this.areaService.getProvs();
		defaultMap.put(Params.AREA_PROVS_PARAM, provs);
//		System.out.println(areaId);
		Area area = (Area) this.areaService.getById(areaId);
		if(StringUtils.isNotBlank(area.getDistrict())) {
			List<Area> cities = this.areaService.getCitesByProvId(area.getProvinceId());
			List<Area> dists = this.areaService.getDistsByCityId(area.getCityId());
			defaultMap.put(Params.AREA_CITIES_PARAM, cities);
			defaultMap.put(Params.AREA_DISTS_PARAM, dists);
		} else if(StringUtils.isNotBlank(area.getCity())) {
			List<Area> cities = this.areaService.getCitesByProvId(area.getProvinceId());
			defaultMap.put(Params.AREA_CITIES_PARAM, cities);
		} else {}
		
		defaultMap.put(Params.AREA_AREA_PARAM, area);
		
		return Params.AREA_DEFAULT_VALUES_STRING;
	}
	
	/**
	 * 后台查询
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listArea() {
		Map<String, Object> paramsMap = null;
		if(queryParam == null) {
			paramsMap = new HashMap<String, Object>();
		} else {
			JSONObject jo = JSONObject.fromObject(queryParam);
			paramsMap = (Map<String, Object>) JSONObject.toBean(jo, HashMap.class);
		}
		paramsMap.put("start", start == null ? 0 : start);
		paramsMap.put("pageSize", Params.BACKGROUND_PAGE_SIZE);
		mesMap = this.areaService.getAreaListByComplex(paramsMap);
		return SUCCESS;
	}
}
